package org.opens.javaskill.design;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

import java.util.ArrayList;
import java.util.List;

/**
 * 说明:
 *      1. 组合模式就是将有层级关系的数据组合成一个树结构.
 */
public class CompositePattern {

    public static void main(String[] args) {
        Menu mainMenu = new Menu(1, "main", new ArrayList<>());

        Menu childMenu1 = new Menu(11, "child1", null);
        Menu childMenu2 = new Menu(12, "child2", null);
        Menu childMenu3 = new Menu(13, "child3", null);

        mainMenu.add(childMenu1);
        mainMenu.add(childMenu2);
        mainMenu.add(childMenu3);

        System.out.println(mainMenu);
    }

}

@AllArgsConstructor
@Data
@NoArgsConstructor
class Menu {

    private Integer id;

    private String menuName;

    private List<Menu> children;

    public void add(Menu menu) {
        this.children.add(menu);
    }

    public void remove(Menu menu) {
        this.children.remove(menu);
    }

}
